<?php

namespace App\Dao;

use App\Exceptions\SysExceptions;
use App\Model\BillModel;
use Illuminate\Support\Facades\DB;

class BillDao extends BaseDao
{
    public static function get_bill_builder($params = []){
        $query = BillModel::query();

        $query->when(checkIsHasField($params,'user_id'),function ($query)use ($params){
            $query->where('user_id',$params['user_id']);
        });


        $query->when(checkIsHasField($params,'pm'),function ($query)use ($params){
            $query->where('pm',$params['pm']);
        });



        $query->when(checkIsHasField($params,'type'),function ($query)use ($params){
            $query->where('type',$params['type']);
        });


        $query->when(checkIsHasField($params,'status'),function ($query)use ($params){
            $query->where('status',$params['status']);
        });

        $query->when(checkIsHasField($params,'bill_id'),function ($query)use ($params){
            $query->where('bill_id',$params['bill_id']);
        });

        return $query;
    }

    public static function get_bills(array $all)
    {
        return self::get_bill_builder($all)->orderByDesc('created_at')->get();
    }

    public static function get_bills_page(array $all)
    {
        return self::get_bill_builder($all)->with('userInfo')->orderByDesc('created_at')->paginate();
    }

    /**
     * @throws SysExceptions
     */
    public static function audit(array $params)
    {
        DB::beginTransaction();
        $res = self::get_bill_builder([
            'bill_id'=>$params['bill_id']
        ])->update([
            'status'=>$params['status'],
            'text_str'=>$params['text_str']
        ]);
        if($params['status'] == 2){
            $billInfo= self::get_bill_builder([
                'bill_id'=>$params['bill_id']
            ])->first();
            //增加回去
            $res !== false && $res = UsersDao::get_user_builder([
                'user_id'=>$billInfo['user_id']
            ])->increment('k_price',$billInfo['num']);
        }
        if($res === false){
            DB::rollBack();
            self::error('审核失败');
        }
        DB::commit();
    }

    public static function get_one(array $all)
    {
        return self::get_bill_builder($all)->first()->toArray();
    }



    public static function moneyNums($user_id,$price,$title,$type,$pm = 1): void
    {
        BillDao::get_bill_builder()->create([
            'user_id'=>$user_id,
            'pm'=>$pm,
            'type'=>$type,
            'num'=>$price,
            'title'=>$title,
            'status'=>1
        ]);
        if($pm == 1){
            UsersDao::get_user_builder([
                'user_id'=>$user_id
            ])->increment('k_price',$price);
        }else{
            UsersDao::get_user_builder([
                'user_id'=>$user_id
            ])->decrement('k_price',$price);
        }
    }

    public static function dMoneyNums($user_id,$price,$title,$type,$pm = 1): void
    {
        BillDao::get_bill_builder()->create([
            'user_id'=>$user_id,
            'pm'=>$pm,
            'type'=>$type,
            'num'=>$price,
            'title'=>$title,
            'status'=>1
        ]);
        if($pm == 1){
            UsersDao::get_user_builder([
                'user_id'=>$user_id
            ])->increment('d_price',$price);
        }else{
            UsersDao::get_user_builder([
                'user_id'=>$user_id
            ])->decrement('d_price',$price);
        }
    }


}
